/**
 * This work is licensed under a GNU General Public License 
 * (http://www.gnu.org/copyleft/gpl.html).
 * 
 * Title: 		Mobup
 * Version: 		0.3
 * Description:         Mobile uploader for Flickr
 * Creator:             Vincenzo Lembo and Matteo Penzo
 * Source:              http://mobup.org
 *
 */

import javax.microedition.rms.*;
import javax.microedition.midlet.*;

/**
 * Configuration Class
 *
 * Class to get and store (key,value) in RecordStore of device
 * The couples (key,value) are stored in record as "#name#value" string
 *
 */
public class Configuration {
  static private RecordStore rs;

  private final static String NAME = "BloglinesConfiguration";

  private Configuration() {

  }
	
  public final static String USERNAME_PARAM = "*username*";
  public final static String PASSWORD_PARAM = "*password*";

  /**
   * Get the value of parameter stored in RecordStore
   */
  public static String Get(String param) {
    String tmpParam = null;
    String tmpVal = null;
    byte[] dataRecord = null;
    String valueRecord = "";

    try {
      rs = RecordStore.openRecordStore(NAME, true);
      //linear search of record
      for (int i = 1; i <= rs.getNumRecords(); i++) {
	tmpParam = "#" + param + "#";
	dataRecord = rs.getRecord(i);
	tmpVal = new String(dataRecord);

	if (tmpVal.indexOf(tmpParam) > -1) {
	  //get only value of the string in record
	  valueRecord = tmpVal.substring(tmpVal.indexOf(tmpParam) + tmpParam.length());
	}
      }
      rs.closeRecordStore();
      System.out.println("GET " + param + " -> " + valueRecord);
      return valueRecord;
    } catch (RecordStoreException ex) {
      return ex.getMessage();
    }

  }

  /**
   * Set the value of parameter stored in RecordStore
   */
  public static String Set(String param, String val) {
    
    System.out.println("SET " + param + " -> " + val);

    String tmpParam = null;
    String tmpVal = null;
    byte[] dataRecord = null;
    String valueRecord = "";

    try {
      rs = RecordStore.openRecordStore(NAME, true);
      //linear search of record
      for (int i = 1; i <= rs.getNumRecords(); i++) {
	tmpParam = "#" + param + "#";
	dataRecord = rs.getRecord(i);
	tmpVal = new String(dataRecord);

	if (tmpVal.indexOf(tmpParam) > -1) {
	  valueRecord = "#" + param + "#" + val;
	  rs.setRecord(i, valueRecord.getBytes(), 0, valueRecord.getBytes().length);
	}
      }
      rs.closeRecordStore();
      if (valueRecord == "") {
	Add(param, val);
      }
      return "";
    } catch (RecordStoreException ex) {
      return ex.getMessage();
    }
  }

  /**
   * Add the value of parameter in RecordStore
   */
  public static String Add(String param, String val) {
    try {
      rs = RecordStore.openRecordStore(NAME, true);
      val = "#" + param + "#" + val;
      //if RecordStore contains x records 
      //the new record is added in x+1 position
      rs.addRecord(val.getBytes(), 0, val.getBytes().length);
      rs.closeRecordStore();
      return "";
    } catch (RecordStoreException ex) {
      return ex.getMessage();
    }
  }

}
